package beastick.utility;

//Copyright 2008 Tofig Kareemov, estereos@gmail.com
//
//Licensed under the Apache License, Version 2.0 (the "License");
//you may not use this file except in compliance with the License.
//You may obtain a copy of the License at
//
//http://www.apache.org/licenses/LICENSE-2.0
//
//Unless required by applicable law or agreed to in writing, software
//distributed under the License is distributed on an "AS IS" BASIS,
//WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//See the License for the specific language governing permissions and
//limitations under the License.

public class CStringParameterizer extends CUtilityMethods
{
 private final char   C_SEPARATOR           = '%';
 private final String S_PARAM_DATEVERSION   = C_SEPARATOR + "DATEVERSION"
                                              + C_SEPARATOR;
 private final String S_PARAM_TIMEVERSION   = C_SEPARATOR + "TIMEVERSION"
                                              + C_SEPARATOR;
 public final String  S_PARAM_SOURCE        = C_SEPARATOR + "SOURCE"
                                              + C_SEPARATOR;
 public final String  S_PARAM_CLASSNAME     = C_SEPARATOR + "CLASS_NAME"
                                              + C_SEPARATOR;
 public final String  S_PARAM_FULLCLASSNAME = C_SEPARATOR + "FULL_CLASS_NAME"
                                              + C_SEPARATOR;
 public final String  S_PARAM_PACKAGENAME   = C_SEPARATOR + "PACKAGE_NAME"
                                              + C_SEPARATOR;
 public final String  S_PARAM_SERVICENUMBER = C_SEPARATOR + "SERVICE_NUMBER"
                                              + C_SEPARATOR;
 private String       sPattern              = "";
 private String       sOutput               = "";
 char[]               cOutput               = new char[0];
 private CDateVersion oDateVersion          = new CDateVersion();

 public CStringParameterizer()
 {
  super();
 }

 public CStringParameterizer(String isPattern)
 {
  super();
  LoadPattern(isPattern);
 }

 public void LoadPattern(String isPattern)
 {
  sPattern = isPattern;
  Set(sPattern);
 };

 public void Reset()
 {
  Set(sPattern);
 };

 public String Get()
 {
  return sOutput;
 };

 public void Set(String sValue)
 {
  sOutput = sValue;
  cOutput = sOutput.toCharArray();
 };

 public void DateVersionCompiled()
 {
  Replace(S_PARAM_DATEVERSION, oDateVersion.GetTodayDateVersion());
 }

 public void TimeVersionCompiled()
 {
  Replace(S_PARAM_TIMEVERSION, oDateVersion.GetTimeVersion());
 }

 public void ParameterCompiled(String sParameter, String sValue)
 {
  Replace(sParameter, sValue);
 }

 public void Replace(String sParameter, String sValue)
 {
  int i = 0;
  int i1 = 0;
  int iLength = 0;
  int iParameterLength = 0;
  boolean bFound = false;
  char[] cParameter = sParameter.toCharArray();
  if (sValue == null)
  {
   sValue = "";
  }
  if (!IsValuable(sParameter))
  {
   return;
  }
  iLength = cOutput.length;
  for (i = 0; i < iLength; ++i)
  {
   if (cOutput[i] == cParameter[0])
   {
    iParameterLength = cParameter.length;
    bFound = true;
    for (i1 = 0; i1 < iParameterLength; ++i1)
    {
     if ((i + i1) < iLength)
     {
      if (cOutput[i + i1] != cParameter[i1])
      {
       bFound = false;
       break;
      }
     } else
     {
      bFound = false;
      break;
     }
    }
    if (bFound)
    {
     Set(sOutput.substring(0, i) + sValue + sOutput.substring(i + i1, iLength));
     i = i + sValue.length();
     iLength = cOutput.length;
    }
   }
  }
 }

 public boolean Contains(String sParameter)
 {
  int i = 0;
  int i1 = 0;
  int iLength = 0;
  int iParameterLength = 0;
  boolean bFound = false;
  char[] cParameter = sParameter.toCharArray();
  if (!IsValuable(sParameter))
  {
   return bFound;
  }
  iLength = cOutput.length;
  for (i = 0; i < iLength; ++i)
  {
   if (cOutput[i] == cParameter[0])
   {
    iParameterLength = cParameter.length;
    bFound = true;
    for (i1 = 0; i1 < iParameterLength; ++i1)
    {
     if ((i + i1) < iLength)
     {
      if (cOutput[i + i1] != cParameter[i1])
      {
       bFound = false;
       break;
      }
     } else
     {
      bFound = false;
      break;
     }
    }
    if (bFound)
    {
     return bFound;
    }
   }
  }
  return bFound;
 }

 public void UnknownCompiled()
 {
  StringBuffer sTemp = new StringBuffer("");
  int i = 0;
  boolean bToCopy = true;
  for (i = 0; i < cOutput.length; ++i)
  {
   if (cOutput[i] == C_SEPARATOR)
   {
    bToCopy = !bToCopy;
   } else if (bToCopy)
   {
    sTemp.append(cOutput[i]);
   }
  }
  Set(sTemp.toString());
 }

 public void UnknownCompiled(String sBraceBegin, String sBraceEnd)
 {
  String sTemp = "";
  int i = 0;
  boolean bToCopy = true;
  int iBegin = -1;
  int iBraceBegin = 0;
  int iBraceEnd = 0;
  int iBraceBeginLength = sBraceBegin.length();
  int iBraceEndLength = sBraceEnd.length();
  boolean bIn = false;
  char c;
  for (i = 0; i < cOutput.length; ++i)
  {
   c = cOutput[i];
   if (!bIn)
   {
    if (c == sBraceBegin.charAt(0))
    {
     bIn = true;
     iBraceBegin = 1;
     iBegin = i;
    }
   } else
   {
    if (iBraceBegin < iBraceBeginLength)
    {
     if (c == sBraceBegin.charAt(iBraceBegin))
     {
      bIn = true;
      ++iBraceBegin;
     } else
     {
      bIn = false;
      iBraceBegin = 0;
      iBraceEnd = 0;
      iBegin = -1;
     }
    } else if (iBraceEnd < iBraceEndLength)
    {
     if (c == sBraceEnd.charAt(iBraceEnd))
     {
      bIn = true;
      ++iBraceEnd;
     } else
     {
      iBraceEnd = 0;
     }
     if ((iBraceEnd == iBraceEndLength) && (bIn) && (iBraceBegin >= 0)
       && (iBraceBegin <= i))
     {
      ++i;
      if (i < (cOutput.length - 1))
      {
       Set(sOutput.substring(0, iBegin)
         + sOutput.substring(i, sOutput.length() - 1));
      } else
      {
       Set(sOutput.substring(0, iBegin));
      }
      i = iBegin - 1;
      bIn = false;
      iBraceBegin = 0;
      iBraceEnd = 0;
      iBegin = -1;
     }
    }
   }
  }
 }
}